#!/usr/bin/env python
#coding:utf-8
import urllib2,re

class Dict:
    """Class to get EN2CN & CN2EN translation from dict.cn"""

    def __init__( self, word="Della" ):
        """initializing the word and the url received"""   
        self.setWord(word)
        self.setUrl()


    def setWord( self, word ):
        """function to set the word"""
        self.__word=word


    def setUrl(self):
        """function to set the url"""
        word=urllib2.quote(self.__word)
        self.__url="http://dict.cn/ws.php?utf8=true&q=%s"%word


    def getPage(self):
        """function to get the content of the web page.return the string page content"""        
        url=self.__url
        page_content=''
        retry=4
        for tim in range(retry):
            try:
                page = urllib2.urlopen(url)
                page_content = page.read()
                page.close()
                break
            except:
                pass
        return page_content


    def getWord(self):
        """function get the info what we needed from the web page.return a list reply"""
        page_content=self.getPage().replace("\n"," ")
        page_content=unicode(page_content,"utf-8") # set the page content encoding to unicode
        regex=r'<def>(.*)</def>' 
        match=re.findall( regex , page_content )
        respon="Oops,你查询的单词不存在或输入格式有误，请检查下或输入'词典'获取帮助"
        if(match==[]):
            respon='服务端连接有误，请稍后再试'
        elif(match[0].encode('utf-8')<>'Not Found'):
            respon='['+self.__word+'] '+match[0].encode('utf-8')
        return [respon]
        
        
        
        
        
        